package net.jsoft.pms.web.mysql.ywfwqd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import net.jsoft.platform.core.web.BaseController;
import net.jsoft.pms.entity.plan.TaskYwfwqd;
import net.jsoft.pms.entity.ywfwqd.YwfwqdEntity;
import net.jsoft.pms.repository.taskywfwqd.TaskYwfwqdRepository;
import net.jsoft.pms.repository.ywfwqd.YwfwqdRepository;
import net.jsoft.pms.repository.ywfwqd.YwfwqdStartupRunner;

@Controller
@RequestMapping("/ywfwqd")
public class YwfwqdController extends BaseController
{
    private String PREFIX = "/pms/mysql/ywfwqd/";

    @Autowired
    YwfwqdRepository repository;

    @Autowired
    TaskYwfwqdRepository taskYwfwqdRepository;

    @RequestMapping("/list")
    public String indexPage()
    {
        return PREFIX + "list";
    }

    @RequestMapping("/ywfwqdTask")
    public String ywfwqdTask()
    {
        return PREFIX + "ywfwqdTask";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(int pageSize, int pageNumber, final YwfwqdEntity ywfwqd)
    {

        List<Object> params = new ArrayList<Object>();

        int result = 0;

        String sql = "select GROUP_CONCAT(py.taskId SEPARATOR ',') taskIds,y.id,y.ywfwid,y.ywfwmc,y.sszb,y.fwljms,y.ywfwxh,y.czlx,y.dwfbzt,y.fwbbh  from t_dzswj_ywfwqdb y left join t_dzswj_plan_ywfwqd py on y.id = py.ywfwqdId where 1=1 ";

        if (StringUtils.isNotEmpty(ywfwqd.getSszb()))
        {
            String sszb = null;
            switch (ywfwqd.getSszb())
            {
            case "BS_ZXBS":
                sszb = "在线办税";
                break;
            case "BS_SBJS":
                sszb = "申报缴税";
                break;
            case "BS_MHQX":
                sszb = "门户权限";
                break;
            case "BS_XXFW":
                sszb = "信息服务";
                break;

            }
            sql += " and sszb = ? ";
            params.add(sszb);
        }

        if (StringUtils.isNotEmpty(ywfwqd.getYwfwid()))
        {

            sql += " and y.ywfwid like ?";
            params.add("%" + ywfwqd.getYwfwid() + "%");
        }
        if (StringUtils.isNotEmpty(ywfwqd.getLrr_dm()))
        {
            sql += " and y.lrr_dm like ?";
            params.add("%" + ywfwqd.getLrr_dm() + "%");
        }

        if (StringUtils.isNotEmpty(ywfwqd.getDwfbzt()))
        {
            sql += " and y.dwfbzt in (";

            List<String> dwfbzts = Arrays.asList(ywfwqd.getDwfbzt().split(","));
            for (int i = 0; i < dwfbzts.size(); i++)
            {
                if (i == 0)
                {
                    sql += " ? ";
                }
                else
                {
                    sql += " ,? ";
                }

            }
            sql += " ) ";
            params.addAll(dwfbzts);
        }

        sql += " group by y.id,y.ywfwid,y.ywfwmc,y.sszb,y.fwljms,y.ywfwxh,y.czlx,y.dwfbzt,y.fwbbh  ";
        sql += " order by y.id desc ";

        String limited = " limit ?,? ";

        String countSql = "select count(1) from (" + sql + ") cc";

        result = this.getTemplate().queryForObject(countSql, params.toArray(), Integer.class);
        params.add((pageNumber - 1) * pageSize);
        params.add(pageSize);

        List<Map<String, Object>> list = this.getTemplate().queryForList(sql + limited, params.toArray());

        for (Map<String, Object> map : list)
        {
            String s = null;
            byte[] taskIdBlob = (byte[]) map.get("taskIds");
            if (taskIdBlob != null)
            {
                s = new String(taskIdBlob);
            }
            map.put("taskId", s);
        }

        JSONObject json = new JSONObject();
        json.put("rows", list); //这里的 rows 和total 的key 是固定的 
        json.put("total", result);
        return json;
    }

    @RequestMapping("/getYwfwxh")
    @ResponseBody
    public String getYwfwxh(YwfwqdEntity ywfwqd)
    {
        String ywfwxh = ywfwqd.getSszb() + "_" + YwfwqdStartupRunner.getNum(ywfwqd.getSszb());
        return ywfwxh;
    }

    @RequestMapping("/add")
    public String addPage()
    {
        return PREFIX + "add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(YwfwqdEntity ywfwqd)
    {
        String returnMsg = "ok";
        YwfwqdEntity temp = repository.findOneByYwfwid(ywfwqd.getYwfwid());
        if (temp != null)
        {
            return "已存在改服务名";
        }
        else
        {
            ywfwqd.setDwfbzt("3");//待发布
            ywfwqd.setLrrq(new Date());
            repository.save(ywfwqd);
        }
        return returnMsg;
    }

    @RequestMapping("/edit")
    public String editPage(YwfwqdEntity ywfwqd, Model model)
    {
        ywfwqd = repository.findOne(ywfwqd.getId());
        model.addAttribute("ywfwqd", ywfwqd);
        return PREFIX + "edit";
    }

    @RequestMapping("/editSave")
    @ResponseBody
    public String editSave(YwfwqdEntity ywfwqd)
    {
        String returnMsg = "ok";
        YwfwqdEntity temp = repository.findOne(ywfwqd.getId());
        if ("1".equals(temp.getDwfbzt()) || "2".equals(temp.getDwfbzt()))
        {
            temp.setDwfbzt("4");//已修改待发布
        }
        temp.setBz(ywfwqd.getBz());
        temp.setQyfwyfwid(ywfwqd.getQyfwyfwid());
        temp.setRequestxsd(ywfwqd.getRequestxsd());
        temp.setResponsexsd(ywfwqd.getResponsexsd());
        temp.setGdslx(ywfwqd.getGdslx());
        temp.setCzlx(ywfwqd.getCzlx());
        temp.setSrcs(ywfwqd.getSrcs());
        temp.setSccs(ywfwqd.getSccs());
        temp.setXgrq(new Date());
        repository.save(temp);
        return returnMsg;
    }

    @RequestMapping("/view")
    public String viewPage(YwfwqdEntity ywfwqd, Model model)
    {
        ywfwqd = repository.findOne(ywfwqd.getId());
        model.addAttribute("ywfwqd", ywfwqd);
        return PREFIX + "view";
    }

    @RequestMapping("/checkTask")
    public String checkTask(YwfwqdEntity ywfwqd, Model model)
    {
        ywfwqd = repository.findOne(ywfwqd.getId());

        String sql = " select y.id,y.taskId,p.name ,p.type , p.earlyStart,p.earlyFinish,p.lateStart,p.lateFinish,p.earlyVersion,p.lateVersion,p.versionDate,u.fullName,p.notes,p.problemId from t_dzswj_plan_ywfwqd  y left join t_dzswj_plan p on y.taskId = p.id join t_security_user u on p.optPerson =  u.id  where  y.ywfwqdId = ? ";
        List<Map<String, Object>> taskIds = this.getTemplate().queryForList(sql, ywfwqd.getId());
        model.addAttribute("ywfwqd", ywfwqd);
        model.addAttribute("taskIds", taskIds);
        return PREFIX + "checkTask";
    }
    @RequestMapping("/delTask")
    @ResponseBody
    public String delTask(long id)
    {
        this.taskYwfwqdRepository.delete(id);
        return "ok";
    }

    @RequestMapping("/saveTask")
    @ResponseBody
    public String saveTask(long ywfwqdId, long id)
    {
        TaskYwfwqd taskYwfwqd = new TaskYwfwqd();
        taskYwfwqd.setTaskId(id);
        taskYwfwqd.setYwfwqdId(ywfwqdId);
        taskYwfwqdRepository.save(taskYwfwqd);
        return "ok";
    }

    @RequestMapping("/published")
    @ResponseBody
    public String published(@RequestParam("ids[]") List<Long> ids)
    {
        String returnMsg = "ok";

        repository.updateDwfwztPublished(ids);

        return returnMsg;
    }

    @RequestMapping("/cancel")
    @ResponseBody
    public String cancel(@RequestParam("ids[]") List<Long> ids)
    {
        String returnMsg = "ok";

        repository.updateDwfwztCancel(ids);

        return returnMsg;
    }

    @RequestMapping("/unCancel")
    @ResponseBody
    public String unCancel(@RequestParam("ids[]") List<Long> ids)
    {
        String returnMsg = "ok";

        repository.updateDwfwztUnCancel(ids);

        return returnMsg;
    }

}
